import {createApp} from 'vue'
import App from './App.vue'
import routes from './router'
import { createRouter, createWebHashHistory } from "vue-router";
import {pinia} from './config'
import piniaPersist from 'pinia-plugin-persist'
import Antd, {message} from 'ant-design-vue';
import '@/style/common.less'
import '@/style/layout.less'
import '@/style/page.less'
import '@/style/default-antdv.less'
import './public-path'
import directiveFn from '@/directive'

// 引入echarts
import * as echarts from 'echarts';

import CommonModal from "@/components/CommonModal/index.vue";
import IpCall from "@/components/IpCall/index.vue";
import CountTo from "@/components/vue-count-to/vue-countTo.vue";
import SQuploadFile from "@/components/uploadFile/uploadFile.vue";
import {dictTypeList} from "@/service/api";
import {startProgress, closeProgress} from '@/utils/progress';
import { useMainStore } from '@/store'

let instance = null;
pinia.use(piniaPersist)

let app = null
function render(props = {}) {
    const {container} = props;
    const rootDomName = process.env.VUE_APP_NAME+"Root"
    app = createApp(App);

    const routerBase = process.env.VUE_APP_ISQIANKUN === 'true' ? `/#/subject/${process.env.VUE_APP_NAME}` : '/'
    const router = createRouter({
        history: createWebHashHistory(routerBase),
        routes,
        scrollBehavior(to, from, savedPosition) {
            return { left: 0, top: 0 }
        }
    });
    router.beforeEach((to, from, next)=>{
        // console.log(to, from, next,999999);

        startProgress();
        if(to.path==='/login'){
            localStorage.removeItem('home_tab_arr')
            next()
        }else{
            let tabArr = JSON.parse(localStorage.getItem('home_tab_arr'))
            if(tabArr&&tabArr.length>=10&&!tabArr.find(r=>r.path===to.path)){
                message.warning('最多打开10个页面')
                return
            }
        }
        
        if(localStorage.getItem('token') && to.path !== '/login') {
            const mainStore = useMainStore()
            if(mainStore.dictTree.length ==0){
                // dictTypeList().then(res=>{
                //     if(res.status =='000_0000_0000') {
                //         mainStore.SET_DICT(res.data)
                //     }else{
                //         message.warning(res.msg)
                //     }
                //     next()
                // }).catch(()=>{
                //     next()
                // })
                next()
            }else{
                next()
            }
            
        }else{
            next()
        }
    })
    router.afterEach(() => {
        closeProgress()
    })
    app.config.globalProperties.$getGlobalState = props?.getGlobalState;
    app.config.globalProperties.$onGlobalStateChange = props?.onGlobalStateChange;
    app.config.globalProperties.$offGlobalStateChange = props?.offGlobalStateChange;
    // app.config.globalProperties.$talking = IpCall;

    // 注册自定义指定
    app.use(directiveFn)
    instance = app
        .use(pinia)
        .use(router)
        .use(Antd)
        .component('CommonModal', CommonModal)
        .component('IpCall', IpCall)
        .component('CountTo', CountTo)
        .component('SQuploadFile', SQuploadFile)
        .mount(container ? container.querySelector(`#${rootDomName}`) : `#${rootDomName}`)
    document.getElementById(rootDomName).style.height = '100%';
}
if (process.env.VUE_APP_ISQIANKUN !== 'true') {
    render();
}

export async function bootstrap() {
    // console.log('[vue] vue app bootstraped');
}

export async function mount(props) {
    // console.log('[vue] props from main framework', props);
    render(props);
}

export async function unmount() {
    instance.$unmounted;
    instance.$el.innerHTML = '';
    instance = null;

    app.unmount();
    app = null;
// console.log(instance)
}
